Debugging with a Device
If you have a phone with which
you want to do your development, you will want to be able to deploy and
debug directly on the phone itself. First you need to connect your
phone to your development machine. The communication with the phone is
all routed through the Zune software. By connecting your device to your
computer, you should also run Zune (if it fails to run automatically).
While the device will continue to operate normally when connected to
Zune, there are several functions which are disabled to allow for Zune
to synchronize media (music, photos and videos) to the device.
Consequently these functions are using your media on the phone. Once
connected, the device looks like it would normally (as seen in Figure 4).
All the communication that
you will do to your phone (e.g. debugging, deploying and registration)
is all done while the Zune software is running. Once you’ve connected
your phone to your computer and run the Zune software, you should be
able to see the phone attached as shown in Figure 5.
Once your device is connected,
you can use it to sync your music, videos and photos to the phone.
Though, before you can use a phone as a development device, you will
need to register the phone for development. This lifts the requirements
that applications that are signed by Microsoft. This allows you to
deploy your applications directly to the phone so you can debug
applications.
Before you can enable your phone as a developer phone, you will need to have an account at the Windows Phone developer portal.
Once you have done that, you can enable your phone to be used for
development. To do this you will need the tool called “Windows Phone
Developer Registration”. This tool is installed when you install the
Windows Phone 7 SDK. When you run this application it will ask you for
your Windows Live ID that you used to register with the developer
portal as shown in Figure 6.
If your phone is successfully
attached to your computer, the Status will tell you that it is ready to
register your device for development. At this point, just hit the
‘Register’ button to register with the developer portal. Once it
registers the phone, it changes the status to show you that the phone
is ready (as shown in Figure 7).
Once you’ve registered your
device, you can deploy and debug your applications using Visual Studio.
The key to using the device instead of the emulator is to change the
deployment using the drop-down list of deployment options. There are
only two:
Windows Phone 7 Emulator
Windows Phone 7 Device
The drop-down is located in the toolbar of Visual Studio as shown in Figure 8.
Once you change the
deployment target you can debug just like you did with the emulator.
When you run the application, it will deploy your application to the
device and run it so you can debug it in the same way as you did with
the emulator. Figure 9 shows the application running on a device.
Using Touch
Even though the touch
interactions do, in fact, fire mouse events there are other events
which allow you to design your application for touch. Since touch is so
important to the way that applications on the phone work, this first
application should give you a taste of that experience. To show touch
working, let’s add an ellipse to the application that the user can move
around by dragging it with their finger. To get started, you should
open the MainPage.xaml file and add a new ellipse in the center of the page. To do this find the TextBlock called theStatus and place a new Ellipse element after it like so:
...
<Grid x:Name="ContentGrid"
Grid.Row="1">
<Rectangle Fill="#FF7E0505"
Margin="8"
RadiusY="24"
RadiusX="24" />
<TextBlock HorizontalAlignment="Center"
TextWrapping="Wrap"
Text="Status"
VerticalAlignment="Bottom"
FontSize="48"
FontWeight="Bold"
Name="theStatus" />
<Ellipse x:Name="theEllipse"
Fill="White"
Width="200"
Height="200">
</Ellipse>
</Grid>
...
We want to be able to move theEllipse
as the user drags it. To allow us to do this we will need to use
something called a Transform. In Silverlight, a Transform is used to
change the way that an object is rendered without having to change
properties of the Ellipse. While we
could change the margins and/or alignments to move it around the
screen, using a transform is much simpler. You should use a TranslateTransform to allow this movement. A TranslateTransform
provides an X and Y property which specify where to draw the element
(as a delta between where it originally exists and where you want it).
You can specify this transform by setting the RenderTransform property with a TranslateTransform (naming it in the process):
...
<Ellipse x:Name="theEllipse"
Fill="White"
Width="200"
Height="200">
<Ellipse.RenderTransform>
<TranslateTransform x:Name="theMover" />
</Ellipse.RenderTransform>
</Ellipse>
...
Now that we have a way to move
our Ellipse around the page, let’s look at dealing with touch. In
Silverlight, there are two specific types of touch interactions that
are meant to allow the user to change on-screen objects. These are when
the user drags their finger on the screen and when they use a pinch
move to resize objects. These types of interactions are called Manipulations. Silverlight has three events to allow you to use this touch information:
ManipulationStarted
ManipulationDelta
ManipulationCompleted
These events let you get information about the manipulation as it happens. For example, lets handle the ManipulationDelta
event to get information about when the user drags on the screen. This
event is called as the manipulation happens and included information
about the difference between the start of the manipulation and the
current state (e.g. how far as the user dragged their finger):
...
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
theStatus.Text = "Hello from Code";
theStatus.MouseLeftButtonUp +=
new MouseButtonEventHandler(theStatus_MouseLeftButtonUp);
theEllipse.ManipulationDelta +=
new EventHandler<ManipulationDeltaEventArgs>(
theEllipse_ManipulationDelta);
}
void theEllipse_ManipulationDelta(object sender,
ManipulationDeltaEventArgs e)
{
// As a manipulation is executed (drag or resize), this is called
theMover.X = e.CumulativeManipulation.Translation.X;
theMover.Y = e.CumulativeManipulation.Translation.Y;
}
...
}
...
The event is fired while
the user either pinches or drags within theEllipse.. In this case the
code is only concerned with the dragging. In the event handler for ManipulationDelta, the ManipulationDeltaEventArgs object contains information about the extent of the manipulation. In the CumulativeManipulation
property of the event args, there is a property called Translation
which contains the extent of the drag operation (the complete delta).
We are just changing theMover’s properties to match the manipulation.
This means we can now drag theEllipse around and see it change position
under our dragging as seen in Figure 10.